//
//  CCVSColumn.m
//  CollectionViewTest
//
//  Created by Pasirstein, David B on 5/11/13.
//  Copyright (c) 2013 Pasirstein, David B. All rights reserved.
//

#import "CCVSColumn.h"

@interface CCVSColumn ()

@property (strong, nonatomic) NSMutableArray * sectionTitles;
@property (strong, nonatomic) NSMutableArray * sectionTopics;
@property (strong, nonatomic) NSMutableArray * sectionDetails;
@end

@implementation CCVSColumn

@synthesize width = _width;
@synthesize percentWidthOfSuperView = _percentWidthOfSuperView;
@synthesize numberOfSections = _numberOfSections;
@synthesize sectionTitles = _sectionTitles;
@synthesize sectionTopics = _sectionTopics;
@synthesize sectionDetails = _sectionDetails;

-(void)setWidth:(CGFloat)width
{
    _percentWidthOfSuperView = 0;
    _width = width;
}

-(void)setPercentWidthOfSuperView:(CGFloat)percentWidthOfSuperView
{
    _width = 0;
    _percentWidthOfSuperView = percentWidthOfSuperView;
}

-(int)numberOfSections
{
    return [self.sectionTitles count];
}

-(int)getNumberOfSectionsFilterEmptySections:(BOOL)filter
{
    if (filter == NO) return [self.sectionTitles count];
    int count = 0;
    for (int i = 0; i < self.numberOfSections; i++) {
        if ((id)self.sectionTopics[i] == [NSNull null]) {
            //skip
            continue;
        } else {
            count++;
        }
    }
    return count;
}


-(NSMutableArray *)sectionTitles
{
    if (!_sectionTitles) _sectionTitles = [[NSMutableArray alloc] init];
    return _sectionTitles;
}

-(NSArray *)getSections
{
    return self.sectionTitles;
}

-(NSArray *)getSectionsFilterEmptySections:(BOOL)filter
{
    if (filter == NO) return [self getSections];
    NSMutableArray * retval = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.numberOfSections; i++) {
        if ((id)self.sectionTopics[i] == [NSNull null]) {
            //skip
            continue;
        } else {
            [retval addObject:self.sectionTitles[i]];
        }
    }
    return retval;
}

-(NSMutableArray *)sectionTopics
{
    if (!_sectionTopics) _sectionTopics = [[NSMutableArray alloc] init];
    return _sectionTopics;
}

-(NSMutableArray *)sectionDetails
{
    if (!_sectionDetails) _sectionDetails = [[NSMutableArray alloc] init];
    return _sectionDetails;
}

-(void)addSection:(NSString*)title withTopics:(NSArray*)topics withDetails:(NSArray*)details
{
    //we only add if a title is provided
    if (title) {
        [self.sectionTitles addObject:title];
        if (topics && (topics != (NSArray*)[NSNull null]) && [topics count] > 0) {
            [self.sectionTopics addObject:topics];
        } else {
            //if we have an empty section (we should allow it
            // but we don't want to crash...so no nil, we
            // need a null object instead
            [self.sectionTopics addObject:[NSNull null]];
        }
        if (details && (details != (NSArray*)[NSNull null]) && [details count] > 0) {
            [self.sectionDetails addObject:details];
        } else {
            //if we have an empty section (we should allow it
            // but we don't want to crash...so no nil, we
            // need a null object instead
            [self.sectionDetails addObject:[NSNull null]];
        }
    }
}

-(void)addSectionAtIndex:(int)section withTitle:(NSString*)title withTopics:(NSArray*)topics withDetails:(NSArray*)details
{
    //first some protection
    section = (section > [self.sectionTitles count]) ? [self.sectionTitles count] : section;
    //we only add if a title is provided
    if (title) {
        [self.sectionTitles insertObject:title atIndex:section];
        if (topics && (topics != (NSArray*)[NSNull null]) && [topics count] > 0) {
            [self.sectionTopics insertObject:topics atIndex:section];
        } else {
            //if we have an empty section (we should allow it
            // but we don't want to crash...so no nil, we
            // need a null object instead
            [self.sectionTopics insertObject:[NSNull null] atIndex:section];
        }
        if (details && (details != (NSArray*)[NSNull null]) && [details count] > 0) {
            [self.sectionDetails insertObject:details atIndex:section];
        } else {
            //if we have an empty section (we should allow it
            // but we don't want to crash...so no nil, we
            // need a null object instead
            [self.sectionDetails insertObject:[NSNull null] atIndex:section];
        }
    }

}

-(void)removeSetion:(int)section
{
    if (section < [self.sectionTitles count]) {
        [self.sectionTitles removeObjectAtIndex:section];
        [self.sectionTopics removeObjectAtIndex:section];
        [self.sectionDetails removeObjectAtIndex:section];
    }
}


//this is the same as add section, but for the future and for now,
//mostly for the programmer, its nice to be clear...
//-(void)addSectionWithSubColumns:(NSString*)title withSubColumns:(NSArray*)ccvsColumns
//{
//    [self addSection:title withData:ccvsColumns];
//}


-(NSString*)titleOfSection:(int)section
{
    return (section < self.numberOfSections) ? self.sectionTitles[section] : nil;
}

-(NSArray*)topicDataInSection:(int)section
{    
    return (section < self.numberOfSections) ? self.sectionTopics[section] : nil;
}

-(NSArray*)detailDataInSection:(int)section
{
    return (section < self.numberOfSections) ? self.sectionDetails[section] : nil;
}

-(NSString*)titleOfSection:(int)section filterEmptySections:(BOOL)filter
{
    //get the quick stuff out of the way
    if (section >= self.numberOfSections) return nil;
    if (filter == NO) return [self titleOfSection:section];
    /// ELSE
    NSString * title = nil;
    for (int i = 0; i < self.numberOfSections; i++) {
        if ((id)self.sectionTopics[i] == [NSNull null]) {
            //skip
            section++;
            continue;
        }
        if (i == section) {
            title = [self titleOfSection:section];
            break;
        }
    }
    return title;
}

-(NSArray*)topicDataInSection:(int)section filterEmptySections:(BOOL)filter
{
    if (section >= self.numberOfSections) return nil;
    if (filter == NO) return [self topicDataInSection:section];
    ///ELSE
    id retval = [NSNull null];
    for (int i = 0; i < self.numberOfSections; i++) {
        if ((id)self.sectionTopics[i] == [NSNull null]) {
            //skip
            section++;
            continue;
        }
        if (i == section) {
            retval = [self topicDataInSection:section];
            break;
        }
    }
    return retval;
}

-(NSArray*)detailDataInSection:(int)section filterEmptySections:(BOOL)filter
{
    if (section >= self.numberOfSections) return nil;
    if (filter == NO) return [self detailDataInSection:section];
    ///ELSE
    id retval = [NSNull null];
    for (int i = 0; i < self.numberOfSections; i++) {
        if ((id)self.sectionTopics[i] == [NSNull null]) {
            //skip
            section++;
            continue;
        }
        if (i == section) {
            retval = [self detailDataInSection:section];
            break;
        }
    }
    return retval;    
}


/*
-(BOOL)doesSectionContainSubColumns:(int)section
{
    BOOL retval = NO;
    //we error protect
    if (section < self.numberOfSections) {
        if ([self.sectionData[section][0] isKindOfClass:[CCVSColumn class]]) {
            retval = YES;
        }
    }    
    return retval;
}
*/

@end







